<?php
class BlogsController extends AppController
{
	var $name = "Blogs";
	var $layout = "default";
	var $components = array('Lastrss','Common','Cookie');
	function index()
	{
		$data = $this->getBloglist();
		$mylist = $this->getMylist();
		$items = array();
		foreach($data as $i=>$blog){
			if(empty($blog['Blog']['rss_url'])){
				continue;
			}
			$tmp = $this->Lastrss->feed($blog['Blog']['rss_url']);
			$rssitem = $tmp['items'][0];
			$rssitem['rss_url'] = urlencode($blog['Blog']['rss_url']);
		 	$items[] = $rssitem;
		}
		$items = $this->Common->sort_by_time($items);
		foreach($items as $k=>$v){
			$items[$k]['pubDate'] = $this->Common->format_date($items[$k]['pubDate']);
			$items[$k]['idx'] = $k;
		}
		Cache::write('blog_list', $items);
		array_splice($items,10);
		$this->set('page_title', "股市录_股票博客_股票行情分析");
		$this->set('blogs',$data);
		$this->set('mylist',$mylist);
		$this->set('rss',$items);
	}
	function leftside()
	{
		$data = $this->getBloglist();
		return $data;
	}
	function view(){
		$url = 'http://'.implode("/",$this->params['pass']);
		$data = $this->Lastrss->feed($url);
		$items = array();
		$curBlog = "";
		foreach($data['items'] as $i=>$val){
			if($i > ($data['items_count']/2)){
				break;
			}
			$val['idx'] = $i;
			$val['pubDate'] = $this->Common->format_date($val['pubDate']);
			$items[] = $val;
		}
		$blogs = $this->getBloglist();
		$mylist = $this->getMylist();
		if(isset($_GET['s'])){
			foreach($mylist as $i=>$blog){
				if($blog['Blog']['rss_url']==$url){
					$mylist[$i]['select'] = true;
					$curBlog = $blog['Blog'];
					break;
				}
			}
		}else{
			foreach($blogs as $i=>$blog){
				if($blog['Blog']['rss_url']==$url){
					$blogs[$i]['select'] = true;
					$curBlog = $blog['Blog'];
					break;
				}
			}
		}
		$this->set('page_title',$curBlog['title']."_股市录");
		$this->set('cur_blog',$curBlog);
		$this->set('blogs',$blogs);
		$this->set('rss',$items);
		$this->set('mylist',$mylist);
		$this->updateViewNum($curBlog['id']);
	}
	function ajaxload()
	{
		if(!isset($_GET['idx']) || !isset($_GET['from'])){
			return;
		}
		$idx = $_GET['idx'];
		$from = $_GET['from'];
		$blogs = $this->getBloglist();
		$this->layout = "ajax";
		if($from=="index"){
			if (($items = Cache::read('blog_list')) !== false) {
					$item = isset($items[$idx+1])?$items[$idx+1]:array();
    		}
			if(empty($item)||empty($items)){
				$this->set('rss',array());
				$this->render('latest_rss');
				return;
			}
			$this->set('rss',array($item));
			$this->render('latest_rss');
			return;
		}else if($from=="view"){
			$url = 'http://'.implode("/",$this->params['pass']);
			$data = $this->Lastrss->feed($url);
			// var_dump($data);
			if(empty($data['items'][$idx+1])){
				$this->set('rss',array());
				$this->render('get_rss');
				return;
			}
			$temp = $data['items'][$idx+1];
			$temp['idx'] = $idx+1;
			$temp['pubDate'] = $this->Common->format_date($temp['pubDate']);
			$this->set('rss',array($temp));
			$this->render('get_rss');
		}
	}
	function mylist(){
		$this->layout="ajax";
		$mylist = $this->getMylist();
		$this->set('mylist',$mylist);
	}
	function newarticle(){
		$this->layout="ajax";
		$data = $this->getBloglist();
		$items = Array();
		foreach($data as $i=>$blog){
			$rssitem = $this->Lastrss->feed($blog['Blog']['rss_url']);
		 	if(!empty($rssitem['items']) && !empty($rssitem['items'][0]) && $rssitem['items'][0]['pubDate']>strtotime("-2 day")){
				array_splice($rssitem["items"],1);
				$rssitem['rss_url'] = urlencode($blog['Blog']['rss_url']);
				$items[] = $rssitem;
			}
			if(count($items)>10){
				break;
			}
		}
		$this->set('data',$items);
	}
	function beforeRender(){
		$this->set('gdata',array('user'=>$this->Cookie->read('user'),
								'rssUrl'=>'http://'.implode("/",$this->params['pass']),
								'accordion_active'=>isset($_GET['s']) ? 1 : 0
								));
	}
	function getBloglist(){
		return $this->Blog->find('all',array('order'=>array("Blog.view_num DESC")));
	}
	function getMylist(){
		$user = $this->Cookie->read('user');
		if(empty($user)){
			return "";
		}
		$result = $this->Blog->find('all',array(
								'joins'=>array(
									array(
                                	'table'=>'blogs_users',
                                	'alias'=>'BlogsUser',
                                	'type'=>'inner',
                      			  	'conditions'=> array('BlogsUser.blog_id = Blog.id','BlogsUser.user_id='.$user['id'])
			                        )
			             )));	
			// var_dump($result);
		return $result;
	}
	/*
	 * 收藏 
	**/
	function stow(){
		$user = $this->Cookie->read('user');
		$this->layout = "ajax";
		if(!ctype_digit($_POST['blog_id']) || empty($user)){
			showMsg("");
			return;
		}
		$conditions = array('user_id'=>$user['id'],'blog_id'=>$_POST['blog_id']);
		if($this->Blog->BlogsUser->find('first',array('conditions'=>$conditions))){
			$this->showMsg("此博客已经在您的收藏里了，不用再添加啦");
			return;
		}
		$this->Blog->BlogsUser->data = $conditions;
		if($this->Blog->BlogsUser->save($this->Blog->BlogsUser->data)){
			$mylist = $this->getMylist();
			$this->set('mylist',$mylist);
			$this->render('/elements/selectoritem');
			return;
		}else{
			$this->showMsg("收藏功能好像出了点问题，请稍后再试");
			return;
		}
	}
	function showMsg($msg){
		$this->set('data',json_encode(array('ret'=>100,'msg'=>$msg)));
	}
	function updateViewNum($id){
		$this->Blog->read(null, $id);
		$num = ++$this->Blog->data['Blog']['view_num'];
		$this->Blog->set('view_num', $num);
		$this->Blog->save();
	}
}
?>